//
//  DB.m
//  HorizonLight
//
//  Created by lanou on 15/9/21.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "DB.h"

@implementation DB

static sqlite3 *db = nil;
//打开数据库
+ (sqlite3 *)open
{
    if (db != nil)
    {
        return db;
    }
    //获取Documents路径
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    //创建一个存放数据库文件的路径
    NSString *sqliPath = [docPath stringByAppendingPathComponent:@"Video.sqlite"];
    //获取存在于包里面的数据库文件
    NSString *bundleFilePath = [[NSBundle mainBundle] pathForResource:@"Video" ofType:@"sqlite"];
    //创建文件管理对象,来执行copy操作
    NSFileManager *fileManger = [NSFileManager defaultManager];
    if ([fileManger fileExistsAtPath:sqliPath] == NO)
    {
        //实现copy操作
        [fileManger moveItemAtPath:bundleFilePath toPath:sqliPath error:NULL];
    }
    //数据库链接使用下面这个函数
    sqlite3_open([sqliPath UTF8String], &db);
    return db;
}
//关闭数据库
+ (void)close
{
    //关闭数据库
    sqlite3_close(db);
    db = nil;//置空指针
}

//根据表明获取数据库中保存的所有信息
+ (NSMutableArray *)allCacheOfDBTable:(NSString *)DBTable
{
    //链接数据库
    sqlite3 *db = [self open];
    int result = 0;
    //创建一个语句对象
    sqlite3_stmt *stmt = nil;
    if ([DBTable isEqualToString:@"CollectList"])
    {
        result = sqlite3_prepare_v2(db, "select * from CollectList", -1, &stmt, NULL);
    }else if ([DBTable isEqualToString:@"CacheList"])
    {
        result = sqlite3_prepare_v2(db, "select * from CacheList", -1, &stmt, NULL);
    }
    NSMutableArray *mArray = nil;
    if (result == SQLITE_OK) {
        mArray = [NSMutableArray arrayWithCapacity:1];
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            //取出每一行的数据需要借助下面函数
            long ID = sqlite3_column_int(stmt, 0);
            const unsigned char *view = sqlite3_column_text(stmt, 1);
            const unsigned char *titleLabel = sqlite3_column_text(stmt, 2);
            //将检索得到的c语言字符串转换成oc语言
            NSString *title = [NSString stringWithUTF8String:(const char *)titleLabel];
            NSString *coverForDetail = [NSString stringWithUTF8String:(const char *)view];

            VideoListModel *model = [[VideoListModel alloc] init];
            model.videoListID = [NSString stringWithFormat:@"%ld",ID];
            model.title = title;
            [mArray addObject:model];

        }
    }
    //不管查询结果成功与否,都要是否stmt所占用的内存空间
    sqlite3_finalize(stmt);//是否
    return mArray;
}
//插入视频
+ (void)insertCacheOfDBTable:(NSString *)DBTable WithModel:(VideoListModel *)model
{
    sqlite3 *db = [self open];
    sqlite3_stmt *stmt = NULL;
    int result = 0;
    if ([DBTable isEqualToString:@"CollectList"]) {
        result = sqlite3_prepare_v2(db, "insert into CollectList(ID,view,titleLabel) values(?,?,?)", -1, &stmt, NULL);
    }else if ([DBTable isEqualToString:@"CacheList"]){
        result = sqlite3_prepare_v2(db, "insert into CacheList(ID,view,titleLabel) values(?,?,?)", -1, &stmt, NULL);
    }
    if (result == SQLITE_OK) {
        sqlite3_bind_int64(stmt, 1, [model.idx integerValue]);
        sqlite3_bind_text(stmt, 2, [model.title UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 3, [model.coverForDetail UTF8String], -1, NULL);
        sqlite3_step(stmt);
    }
    sqlite3_finalize(stmt);

}
//删除指定ID下的视频
+ (void)deleteOfDBTable:(NSString *)DBTable ByID:(int)videoListID
{
    //链接数据库
    sqlite3 *db = [self open];
    sqlite3_stmt *stmt = NULL;
    int result = 0;
    if ([DBTable isEqualToString:@"CollectList"]) {
        result = sqlite3_prepare_v2(db, "delete from CollectList where ID = ?", -1, &stmt, NULL);
    }
    else if ([DBTable isEqualToString:@"CacheList"]){
        result = sqlite3_prepare_v2(db, "delete from CacheList where ID = ?", -1, &stmt, NULL);
    
    }
    if (result == SQLITE_OK) {
        sqlite3_bind_int64(stmt, 1, videoListID);
        sqlite3_step(stmt);
    }
    sqlite3_finalize(stmt);


}


@end
